package com.bgs.travelapp.area.service.Impl;

import com.bgs.travelapp.area.entity.AreaEntity;
import com.bgs.travelapp.area.mapper.AreaMapper;
import com.bgs.travelapp.area.service.AreaService;
import com.bgs.travelapp.scenic.entity.ScenicEntity;
import com.bgs.travelapp.util.RedisUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaServiceImpl implements AreaService {

    @Resource
    AreaMapper areaMapper;

    @Resource
    RedisUtil redisUtil;


    /**
     * 查询所有省份
     * 前端展示页--旅游
     * gpd 2022-07-05 11:26:00
     * @return
     */
    @Override
    public Map<Object, Object> getProvinceAll() {
        AreaEntity areaEntity = new AreaEntity();
        areaEntity.setPid(0);
        List<AreaEntity> areaEntities = areaMapper.select(areaEntity);
        for (AreaEntity entity : areaEntities) {
            AreaEntity areaEntity1 = new AreaEntity();
            areaEntity1.setPid(entity.getId());
            List<AreaEntity> select = areaMapper.select(areaEntity1);
            entity.setChilds(select);
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("diySlide",areaEntities);
        return map;
    }


    /**
     * 全查城市树（后端管理树展示）
     * @return
     */
    @Override
    public Map<Object, Object> getAreaTreeAll() {
        AreaEntity areaEntity = new AreaEntity();
        areaEntity.setPid(0);
        List<AreaEntity> areaEntities = areaMapper.select(areaEntity);
        for(AreaEntity entity : areaEntities){
            areaEntity.setPid(entity.getId());
            List<AreaEntity> areaEntities1 = areaMapper.select(areaEntity);
            entity.setChilds(areaEntities1);
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("diySide",areaEntities);
        return map;
    }

    /**
     * 鼠标移入省份时查询所有市区
     * 前端展示页--旅游
     * 根据pid进行查询
     * gpd 2022-07-05 10:26:00
     * @return
     */
    @Override
    public List<AreaEntity> getCityAllByPid(Integer pid) {
        AreaEntity areaEntity  =new AreaEntity();
        areaEntity.setPid(pid);
        return areaMapper.select(areaEntity);
    }

    /**
     * 获取地区根据Pid
     * hbx 2022-7-12 19:30:53
     * @param pId
     * @return
     */
    @Override
    public List<AreaEntity> getNameByPid(Integer pId) {
        AreaEntity areaEntity = new AreaEntity();
        areaEntity.setPid(pId);
        List<AreaEntity> select = areaMapper.select(areaEntity);
        return select;
    }


    /**
     * 根据首字母获取市（前台展示）
     * lzy 2022-7-15 08点37分
     * @param first
     * @return
     */
    @Override
    public Map<Object, Object> getCitysByFirst(String first) {
        String[] split = first.split(",");
        AreaEntity areaEntity = new AreaEntity();
        areaEntity.setLevel(2);
        Map<Object, Object> map = new HashMap<>();
        for (String s : split) {
            Object o = redisUtil.get(s);
            if(o == null){
                areaEntity.setFirst(s);
                o = areaMapper.select(areaEntity);
                redisUtil.set(s,o);
            }
            map.put(s,o);
        }
        return map;
    }

    /**
     * 根据城市id查询城市名称 （前端 天气）
     * hbx 2022年8月24日15:21:54
     * @param cityId
     * @return
     */
    @Override
    public List<AreaEntity> getCityNameByCityId(Integer cityId) {
        Example example=new Example(AreaEntity.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andLike("id","%"+cityId+"%");
        List<AreaEntity> areaEntities =areaMapper.selectByExample(example);
        return areaEntities;
    }

}
